/*!
 * \file jxbankagree_header.c
 *
 * \brief JXBANKAGREE消息头操作
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.
 *
 * \version 1.0
 * \author songbb
 * \date 2017/04/20
 *
 */

#include <stdio.h>
#include <ctype.h>

#include "frct_comm.h"

#include "jxbankagree_comm.h"
#include "jxbankagree_header.h"
#include "config.h"

int jxbankagree_header_detect(char *p_payload
                             , unsigned int ui_payload_len)
{
    JXBANKAGREE_PRINT_INFO("jxbankagree_header_detect...\n");
    int i_loop = 0;

    if (ui_payload_len < APM_JXBANKAGREE_REQ_HDR_LEN)
        goto unknown;

    for (; i_loop < 8; i_loop++)
    {
        if (!isdigit(*(p_payload + i_loop)))
            goto unknown;
    }

    // 校验Version字段
    if (memcmp(p_payload + 8, "\x20\x20", 2) != 0)
        goto unknown;

    // 校验ApplicationID字段
    if (memcmp(p_payload + 10, "\x30\x30\x30\x30\x30\x32\x20\x20", 8) != 0)
        goto unknown;

    return APM_L7_IS_YES;
unknown:
    return APM_L7_IS_UNKNOWN;
}

int jxbankagree_header_unpack(char *p_payload
                            , unsigned int ui_payload_len
                            , Jxbankagree_header *p_st_jxbankagree_header)
{
    char arr_len[8+1] = {0};
    unsigned long long ull_len = 0LL;

    if (ui_payload_len < APM_JXBANKAGREE_REQ_HDR_LEN)
        goto error;

    memset(arr_len, 0x00, sizeof(arr_len));
    strncpy(arr_len, p_payload, 8);
    ull_len = strtoull(arr_len, NULL, 10);

    p_st_jxbankagree_header->ui_pkt_len = (unsigned int) ull_len + 8;

    return FR_SUCC;
error:
    return FR_FAIL;
}
